SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE VIEW [dbo].[vw_ResumenPOFv0]
AS
SELECT O.Nombre Organizacion, O.IdOrganizaciones, O.CUE, 
    O.CUISE,' '+ pp.mnemo+'-Subsidiado '+Convert(Varchar, IsNull(PorcentajeSubsidio,0))+'%' Partida, 
    RS.Descripcion RegimenSalarial, convert(varchar,c.idcargo)+'-'+convert(varchar,C.Cargo) Cargo, 
    CASE RS.Descripcion WHEN 'Cargo' THEN COUNT(*) 
    WHEN 'Horas' THEN SUM(SA.Horas) END AS Cantidad, 
    CASE RS.Descripcion WHEN 'Cargo' THEN COUNT(*)*c.puntos 
    WHEN 'Horas' THEN SUM(SA.Horas)*c.puntos END AS CantPuntos
FROM Tb_CargosSalariales C
INNER JOIN  tb_ServiciosAgentes SA ON C.IdCargo = SA.CargoSalarial 
INNER JOIN tb_RegimenSalarial RS ON RS.IdRegimenSalarial = C.RegimenSalarial
INNER JOIN tb_SubServiciosAgentes SS On idServicioAgente = ServicioAgente
INNER JOIN tb_Plazas P On idPlaza = Plaza
INNER JOIN tb_PartidasPresupuestarias PP ON p.Presupuesto = PP.IdPartida 
INNER JOIN tb_Organizaciones O ON O.IdOrganizaciones = p.organizacion 
WHERE TipoDEServicio = 1
and pp.idpartida<>4 
 AND (SA.RegimenLaboral = 12 OR
    SA.regimenlaboral = 17 OR
    SA.RegimenLaboral = 16) AND (SA.SituacionRevista = 1 OR
    SA.SituacionRevista = 2) AND GetDate() BETWEEN 
    SA.FechaAlta AND ISNULL(Sa.FechaBaja, GetDate())
and ss.baja= 'N'
   -- AND SS.FechaAlta = (Select Min(FechaAlta) From tb_SubServiciosAgentes Where ServicioAgente = SA.idServicioAgente)
GROUP BY O.Nombre, O.IdOrganizaciones, O.CUE, O.Cuise, 
    P.PorcentajeSubsidio, RS.Descripcion,c.idcargo, C.Cargo,pp.mnemo,pp.idpartida,c.puntos

UNION --plazas sin titulares o interinos (o sea vacias o con suplentes unicamente)

SELECT O.Nombre Organizacion, O.IdOrganizaciones, O.CUE, O.CUISE, 'SinTitOInt-'+pp.mnemo+'-Subsidiado '+Convert(Varchar, IsNull(PorcentajeSubsidio,0))+'%' Partida, 
    RS.Descripcion RegimenSalarial,  convert(varchar,c.idcargo)+'-'+convert(varchar,C.Cargo) Cargo,  CASE RS.Descripcion WHEN 'Cargo' THEN COUNT(*) WHEN 'Horas' THEN SUM(p.Horas) END AS Cantidad,
    CASE RS.Descripcion WHEN 'Cargo' THEN COUNT(*)*c.puntos 
    WHEN 'Horas' THEN SUM(p.Horas)*c.puntos END AS CantPuntos

FROM Tb_CargosSalariales C INNER JOIN  tb_plazas P ON C.IdCargo = p.CargoSalarialDefault 
INNER JOIN tb_PartidasPresupuestarias PP ON p.Presupuesto = PP.IdPartida 
INNER JOIN tb_RegimenSalarial RS ON RS.IdRegimenSalarial = C.RegimenSalarial
INNER JOIN tb_Organizaciones O ON O.IdOrganizaciones = p.organizacion 
WHERE
GetDate() BETWEEN p.FechaAlta 
AND ISNULL(p.FechaBaja, GetDate())
and pp.idpartida<>4 
and p.idplaza not in (select plaza from
              tb_subserviciosagentes ss inner join tb_serviciosagentes sa on sa.idservicioagente=ss.servicioagente
              where sa.TipoDEServicio = 1 AND
			 (SA.RegimenLaboral = 12 OR SA.regimenlaboral = 17 OR SA.RegimenLaboral = 16) 
		    and (SA.SituacionRevista = 1 OR  SA.SituacionRevista = 2)
		    AND GetDate() BETWEEN sa.FechaAlta AND ISNULL(sa.FechaBaja, GetDate())
		  and ss.baja= 'N'
              )


GROUP BY O.Nombre, O.IdOrganizaciones, O.CUE, O.Cuise, P.PorcentajeSubsidio, RS.Descripcion, C.Cargo,  c.idcargo,pp.mnemo,pp.idpartida,c.puntos

--ORDER BY O.CUISE



















GO
GRANT SELECT ON  [dbo].[vw_ResumenPOFv0] TO [SoloVer]
GO
EXEC sp_addextendedproperty N'MS_DiagramPane1', N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[5] 4[4] 2[70] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1[50] 2[25] 3) )"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1 [56] 4 [18] 2))"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      RowHeights = 220
      Begin ColumnWidths = 10
         Width = 284
         Width = 1440
         Width = 1440
         Width = 1440
         Width = 1440
         Width = 1440
         Width = 1440
         Width = 1440
         Width = 1440
         Width = 1440
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
', 'USER', N'dbo', 'VIEW', N'vw_ResumenPOFv0', NULL, NULL
GO
DECLARE @xp int
SELECT @xp=1
EXEC sp_addextendedproperty N'MS_DiagramPaneCount', @xp, 'USER', N'dbo', 'VIEW', N'vw_ResumenPOFv0', NULL, NULL
GO
